package org.mili.ticketeer.ui.ticket;

import org.mili.ticketeer.dto.Action;
import org.mili.ticketeer.dto.Ticket;
import org.mili.ticketeer.service.ActionService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.service.TicketService;
import org.mili.ticketeer.ui.OneToManyForm;
import org.mili.ticketeer.util.Log;

/**
 * Created with IntelliJ IDEA.
 * User: papa
 * Date: 20.07.13
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 */
public class TicketActionForm extends OneToManyForm<Ticket, Action> {
    private TicketService ticketService = ServiceFactory.getService(TicketService.class);
    private ActionService actionService = ServiceFactory.getService(ActionService.class);

    public TicketActionForm(String label) {
        super(label, new TicketActionTableModel(), new ActionDataForm("Action"));
    }

    @Override
    public Action create(Ticket ticket) {
        Log.log(this, "create", "ticket: %s", ticket);
        Action action = new Action();
        action.setTicket(ticket.getId());
        return action;
    }

    @Override
    public void delete(Action element) throws ServiceException {
        Log.log(this, "delete", "action: %s", element);
        actionService.delete(element);
    }

    @Override
    public void onSelectionChanged(Object element) {
        Log.log(this, "onSelectionChanged", "element: %s", element);
        if (element instanceof Ticket) {
            Ticket ticket = (Ticket) element;
            updateOneFormObject(ticket);
        }
    }

}
